package com.jinlejia.feedback.idea.ui;

import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.view.View;

import com.jinlejia.feedback.R;
import com.jinlejia.feedback.common.databinding.CommonEvent;
import com.jinlejia.feedback.databinding.FbActivityIdeaMainBinding;
import com.sevenheaven.segmentcontrol.SegmentControl;

//意见反馈 首页
public class FbIdeaMainActivity extends FragmentActivity {
    private FbActivityIdeaMainBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.fb_activity_idea_main);
        binding.setCommonEvent(CommonEvent.obtain());
        binding.setEvent(this);
        initView();
    }

    private int segmentIndex;
    private void initView() {
        chooseSegmentIndex(segmentIndex);
        binding.segmentControl.setOnSegmentControlClickListener(new SegmentControl.OnSegmentControlClickListener() {
            @Override
            public void onSegmentControlClick(int index) {
                segmentIndex = index;
                chooseSegmentIndex(segmentIndex);
            }
        });
    }
    FragmentManager fm;
    private void chooseSegmentIndex(int index){
        fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        switch (index) {
            case 0: //全部
                ft.replace(R.id.content, new FbIdeaAllFragment(),"FbIdeaAllFragment");
                ft.commitAllowingStateLoss();
                break;
            case 1: //关注
                ft.replace(R.id.content, new FbIdeaAttentionFragment(),"FbIdeaAttentionFragment");
                ft.commitAllowingStateLoss();
                break;
            case 2: //我的
                ft.replace(R.id.content, new FbIdeaMineFragment(),"FbIdeaMineFragment");
                ft.commitAllowingStateLoss();
                break;
            default:
                break;
        }
    }

    public void search(View view){
        Intent intent;
        switch (segmentIndex){
            case 0: //搜索全部
                intent = new Intent(this,FbsearchActivity.class);
                startActivityForResult(intent,103);
                break;
            case 1: //搜索关注
                intent = new Intent(this,FbsearchActivity.class);
                startActivityForResult(intent,104);
                break;
            case 2: //搜索我的
                intent = new Intent(this,FbsearchActivity.class);
                startActivityForResult(intent,105);
                break;
        }
    }


    public void Putquestionsto(View view){
        Intent intent = new Intent(this,PutQuestionsToActivity.class);
        startActivity(intent);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==103){
            Fragment FbIdeaAllFragment = fm.findFragmentByTag("FbIdeaAllFragment");
            FbIdeaAllFragment.onActivityResult(requestCode, resultCode, data);
        }else if (requestCode==104){
            Fragment fbIdeaAttentionFragment = fm.findFragmentByTag("FbIdeaAttentionFragment");
            fbIdeaAttentionFragment.onActivityResult(requestCode, resultCode, data);
        }else if (requestCode==105){
            Fragment fbIdeaMineFragment = fm.findFragmentByTag("FbIdeaMineFragment");
            fbIdeaMineFragment.onActivityResult(requestCode, resultCode, data);
        }
    }
}
